package com.yikaiye.android.yikaiye.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.application.MyApplication;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ItemOrderLayout2Binding;
import com.yikaiye.android.yikaiye.di.retrofit.ApiConstants;
import com.yikaiye.android.yikaiye.model.OrderProductModel;
import com.yikaiye.android.yikaiye.ui.activity.order.OrderDetailsActivity;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.DateUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.ToastHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OrderStatusAdapter extends BaseQuickAdapter<OrderProductModel, DataBindingHolder<ItemOrderLayout2Binding>> {

    private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final Context context;
    private OnBtnStatusListener onBtnStatusListener;
    private OnMoreMenuListener onMoreMenuListener;
    private OnProtocolListener onProtocolListener;

    public OrderStatusAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemOrderLayout2Binding> itemOrderLayoutBindingDataBindingHolder, int i, @Nullable OrderProductModel model) {
        ItemOrderLayout2Binding binding = itemOrderLayoutBindingDataBindingHolder.getBinding();
        if (model != null) {
            binding.tvProductName.setText(CommonUtil.checkText(model.getProductName()));
            binding.tvRealPrice.setText(CommonUtil.checkText(model.getEnterState() == 1 ? model.getPrice() : model.getAppPrice()));
            binding.include.tvProductDes.setText(CommonUtil.checkText(model.getSlogan()));
            Glide.with(MyApplication.getContext()).load(ApiConstants.BASE_IMAGE_RELEASE_URL + model.getAppBanner()).placeholder(R.mipmap.default_image).error(R.mipmap.default_image).into(binding.include.ivProductImg);
            binding.include.tvProductPrice.setText(context.getString(R.string.price, model.getPrice()));
            // 订单状态，0：待付款，1：已付款，2：已取消，3：已退款，4：超时
            switch (model.getStatus()) {
                case 0: // 0：待支付
                    binding.tvOrderStatus.setText(context.getString(R.string.wait_payment));
                    binding.tvOrderStatus.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                    binding.llBottomButton.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                    binding.tvProtocol.setVisibility(View.GONE);
                    binding.tvToPay.setVisibility(View.VISIBLE);
                    binding.tvBtnStatus.setText(context.getString(R.string.cancel_order));
                    // source  0:线上订单，1：线下订单.待付款线下订单（非预订单）的订单，不显示“取消订单”按钮
                    binding.tvBtnStatus.setVisibility(model.getSource() == 0 ? View.VISIBLE : View.GONE);
                    try {
                        // 计算倒计时的总时间
                        long countdownTime = df.parse(model.getExpirationTime()).getTime() - new Date().getTime();
                        model.setTotalTime(countdownTime);
                        binding.tvToPay.setText(model.getTotalTime() > 0 ? context.getString(R.string.to_pay, DateUtil.getTime(model.getTotalTime())) : context.getString(R.string.go_pay));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    break;
                case 1: // 1：已付款（contractSignStatus--> 0：未签署；1：签署处理中；2:签署成功，3:签署失败）
                    binding.tvOrderStatus.setText(context.getString(R.string.already_pay));
                    binding.tvOrderStatus.setTextColor(ContextCompat.getColor(context, R.color.black));
                    binding.llBottomButton.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                    binding.tvBtnStatus.setVisibility(View.VISIBLE);
                    binding.tvBtnStatus.setText(context.getString(R.string.look_progress));
                    binding.tvToPay.setVisibility(View.GONE);
                    // showContract为1时，正常显示合同相关信息，比如签署，查看等等，反之不显示合同相关信息
                    if (model.getShowContract() != null && model.getShowContract() == 1) {
                        // 合同签署状态，0：未签署；1：签署处理中；2:签署成功，3:签署失败
                        if (model.getContractSignStatusNew() != null) {
                            if (2 == model.getContractSignStatusNew()) {
                                binding.tvProtocol.setVisibility(View.VISIBLE);
                                binding.tvProtocol.setText(context.getString(R.string.look_e_agreement));
                                binding.tvProtocol.setTextColor(ContextCompat.getColor(context, R.color.color_3b3a40));
                                binding.tvProtocol.setBackgroundResource(R.drawable.shape_stroke_d7d7d7_4dp);
                            } else {
                                binding.tvProtocol.setVisibility(View.VISIBLE);
                                binding.tvProtocol.setText(context.getString(R.string.sign_agreements));
                                binding.tvProtocol.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                                binding.tvProtocol.setBackgroundResource(R.drawable.shape_stroke_f93f50_4dp);
                            }
                        } else {
                            binding.tvProtocol.setVisibility(View.GONE);
                        }
                    } else {
                        binding.tvProtocol.setVisibility(View.GONE);
                    }

                    break;
                case 2: // 2：已取消
                    binding.tvOrderStatus.setText(context.getString(R.string.already_cancel));
                    binding.tvOrderStatus.setTextColor(ContextCompat.getColor(context, R.color.black));
                    binding.llBottomButton.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                    binding.tvBtnStatus.setVisibility(View.VISIBLE);
                    binding.tvBtnStatus.setText(context.getString(R.string.order_again));
                    binding.tvToPay.setVisibility(View.GONE);
                    binding.tvProtocol.setVisibility(View.GONE);
                    break;
                case 4: // 4：超时
                    binding.tvOrderStatus.setText(context.getString(R.string.timeout));
                    binding.tvOrderStatus.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                    binding.llBottomButton.setVisibility(View.GONE);
                    break;
            }

            binding.tvMore.setOnClickListener(view -> onMoreMenuListener.lookMore(model, i, view));
            binding.tvBtnStatus.setOnClickListener(view -> {
                int btnTag = 0;
                if (binding.tvBtnStatus.getText().toString().equals(context.getString(R.string.cancel_order))) {
                    btnTag = 1;
                } else if (binding.tvBtnStatus.getText().toString().equals(context.getString(R.string.look_progress))) {
                    btnTag = 2;
                } else if (binding.tvBtnStatus.getText().toString().equals(context.getString(R.string.order_again))) {
                    btnTag = 3;
                }
                onBtnStatusListener.onBtnStatusListener(model, i, btnTag);
            });
            binding.tvProtocol.setOnClickListener(view -> {
                int protocolTag = 0;
                if (binding.tvProtocol.getText().toString().equals(context.getString(R.string.sign_e_agreement))) {
                    protocolTag = 1;
                } else if (binding.tvProtocol.getText().toString().equals(context.getString(R.string.look_e_agreement))) {
                    protocolTag = 2;
                }
                onProtocolListener.onProtocolListener(model, i, protocolTag);
            });
            binding.item.setOnClickListener(view -> {
                if (!TextUtils.isEmpty(model.getSlogan())) {
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.ID_KEY, model.getId());
                    bundle.putString(Constant.FROM_KEY, Constant.orderList);
                    JumpIntent.jump((Activity) context, OrderDetailsActivity.class, bundle);
                } else {
                    ToastHelper.show(context.getString(R.string.product_down));
                }
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemOrderLayout2Binding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_order_layout2, viewGroup);
    }

    /**
     * tag:1取消订单、2查看进度、3重新下单
     */
    public interface OnBtnStatusListener {
        void onBtnStatusListener(OrderProductModel model, int position, int tag);
    }

    public void setOnBtnStatusListener(OnBtnStatusListener onBtnStatusListener) {
        this.onBtnStatusListener = onBtnStatusListener;
    }

    /**
     * 更多弹窗
     */
    public interface OnMoreMenuListener {
        void lookMore(OrderProductModel model, int position, View view);
    }

    public void setOnMoreMenuListener(OnMoreMenuListener onMoreMenuListener) {
        this.onMoreMenuListener = onMoreMenuListener;
    }

    /**
     * tag:1签署电子协议/2查看电子协议
     */
    public interface OnProtocolListener {
        void onProtocolListener(OrderProductModel model, int position, int tag);
    }

    public void setOnProtocolListener(OnProtocolListener onProtocolListener) {
        this.onProtocolListener = onProtocolListener;
    }
}
